package cn.bjit.controller;


import cn.bjit.enums.BizCodeEnum;
import cn.bjit.request.LockCouponRecordRequest;
import cn.bjit.service.CouponRecordService;
import cn.bjit.util.JsonData;
import cn.bjit.vo.CouponRecordVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yx
 * @since 2022-06-14
 */
@RestController
@RequestMapping("/api/coupon_record/v1")
public class CouponRecordController {
    @Resource
    private CouponRecordService couponRecordService;

    //分页查询优惠券领取记录
    @ApiOperation("分页查询优惠券领取记录")
    @GetMapping("page")
    public JsonData page(
            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示多少条") @RequestParam(value = "size", defaultValue = "5") int size) {
        Map<String, Object> pageResult = couponRecordService.page(page, size);
        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("根据id查询个人优惠券详情")
    @GetMapping("detail/{record_id}")
    public JsonData getCouponRecordDetail(@ApiParam(value = "查询的id") @PathVariable("record_id") long recordId) {
        CouponRecordVO couponRecordVO = couponRecordService.findById(recordId);
        return couponRecordVO == null ? JsonData.buildResult(BizCodeEnum.COUPON_NO_EXITS) : JsonData.buildSuccess(couponRecordVO);
    }

    @ApiOperation("RPC-锁定优惠券记录")
    @PostMapping("lock_record")
    public JsonData lockCouponRecord(@ApiParam("锁定优惠券的请求对象") @RequestBody LockCouponRecordRequest lockCouponRecordRequest) {
        JsonData jsonData = couponRecordService.lockCouponRecords(lockCouponRecordRequest);
        return jsonData;
    }

}

